﻿using System;
using DevExpress.Xpf.Ribbon;
using Loki.Utils;

namespace Loki.UI.Wpf
{
    /// <summary>
    /// Logique d'interaction pour RibbonViewWindow.xaml
    /// </summary>
    public partial class RibbonViewWindow<TViewModel> : DXRibbonWindow, IView, IView<TViewModel> where TViewModel : class, IViewModel
    {
        #region Log

        /// <summary>
        /// Gets the logger.
        /// </summary>
        /// <value>The logger.</value>
        protected ILog Log
        {
            get
            {
                return _LokiAdapter.CommonLog;
            }
        }

        #endregion Log

        private WindowAdapter<RibbonViewWindow<TViewModel>, TViewModel> _LokiAdapter;

        public RibbonViewWindow()
        {
            _LokiAdapter = new WindowAdapter<RibbonViewWindow<TViewModel>, TViewModel>(this);
            this.Unloaded += ViewWindow_Unloaded;
        }

        private void ViewWindow_Unloaded(object sender, System.Windows.RoutedEventArgs e)
        {
            OnClean(EventArgs.Empty);
        }

        #region Clean

        public event EventHandler Clean;

        protected virtual void OnClean(EventArgs e)
        {
            EventHandler L_Handler = Clean;

            if (L_Handler != null)
            {
                L_Handler(this, e);
            }
        }

        #endregion Clean

        public TViewModel ViewModel
        {
            get { return DataContext as TViewModel; }
            set { DataContext = value; }
        }

        #region Disposed

        public event EventHandler Disposed;

        protected virtual void OnDisposed(EventArgs e)
        {
            EventHandler L_Handler = Disposed;

            if (L_Handler != null)
            {
                L_Handler(this, e);
            }
        }

        #endregion Disposed

        #region Dispose

        /// <summary>
        /// Releases all resources used by an instance of the <see cref="ViewWindow" /> class.
        /// </summary>
        /// <remarks>
        /// This method calls the virtual <see cref="Dispose(bool)" /> method, passing in
        /// <strong>true</strong>, and then suppresses finalization of the instance.
        /// </remarks>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
            OnDisposed(EventArgs.Empty);
        }

        ~RibbonViewWindow()
        {
            Dispose(false);
        }

        /// <summary>
        /// Releases the unmanaged resources used by an instance of the
        /// <see cref="RibbonViewWindow" /> class and optionally releases the managed resources.
        /// </summary>
        /// <param name="disposing"><strong>true</strong> to release both managed and unmanaged
        /// resources; <strong>false</strong> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool P_Disposing)
        {
            if (P_Disposing)
            {
                _LokiAdapter.Dispose();
            }
        }

        #endregion Dispose

        IViewModel IView.ViewModel
        {
            get { return ViewModel; }
            set { ViewModel = value as TViewModel; }
        }
    }
}